import * as Blockly from 'blockly/core';

const blocks = {};

const BLOCK_TOOL_XML_BLOCK_HUE = 65;
const BLOCK_TOOL_XML_VALUE_HUE = 65;
const BLOCK_TOOL_XML_FIELD_HUE = 65;

blocks.block_tool_xml_value = {
    init: function () {
        this.appendDummyInput()
            .setAlign(Blockly.ALIGN_LEFT)
            .appendField("<value name=\"")
            .appendField(new Blockly.FieldTextInput("NAME"), "NAME")
            .appendField("\">");
        this.appendStatementInput("CHILD")
            .setCheck('BLOCK_TOOL_XML_BLOCK')
            .setAlign(Blockly.ALIGN_LEFT);
        this.appendDummyInput()
            .setAlign(Blockly.ALIGN_LEFT)
            .appendField("</value>");
        this.setInputsInline(true);
        this.setPreviousStatement(true, 'BLOCK_TOOL_MAIN_XML');
        this.setNextStatement(true, 'BLOCK_TOOL_MAIN_XML');
        this.setColour(BLOCK_TOOL_XML_VALUE_HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    onchange: function () {
        const parent = this.getSurroundParent();
        let check = false;
        if (parent && [
            'block_tool_main',
            'block_tool_xml_block',
            'block_tool_xml_shadow'
        ].includes(parent.type)) {
            check = true;
        } else {
            check = false;
        }
        if (check) {
            this.setWarningText(null);
        } else {
            this.setWarningText("此块需放到XML、block或shadow下面");
        }
    }
};

blocks.block_tool_xml_statement = {
    init: function () {
        this.appendDummyInput()
            .setAlign(Blockly.ALIGN_LEFT)
            .appendField("<statement name=\"")
            .appendField(new Blockly.FieldTextInput("NAME"), "NAME")
            .appendField("\">");
        this.appendStatementInput("CHILD")
            .setCheck('BLOCK_TOOL_XML_BLOCK')
            .setAlign(Blockly.ALIGN_LEFT);
        this.appendDummyInput()
            .setAlign(Blockly.ALIGN_LEFT)
            .appendField("</statement>");
        this.setInputsInline(true);
        this.setPreviousStatement(true, 'BLOCK_TOOL_MAIN_XML');
        this.setNextStatement(true, 'BLOCK_TOOL_MAIN_XML');
        this.setColour(BLOCK_TOOL_XML_VALUE_HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    onchange: function () {
        const parent = this.getSurroundParent();
        let check = false;
        if (parent && [
            'block_tool_main',
            'block_tool_xml_block',
            'block_tool_xml_shadow'
        ].includes(parent.type)) {
            check = true;
        } else {
            check = false;
        }
        if (check) {
            this.setWarningText(null);
        } else {
            this.setWarningText("此块需放到XML、block或shadow下面");
        }
    }
};

blocks.block_tool_xml_block = {
    init: function () {
        this.appendDummyInput()
            .setAlign(Blockly.ALIGN_LEFT)
            .appendField("<block type=\"")
            .appendField(new Blockly.FieldTextInput("math_number"), "TYPE")
            .appendField("\">");
        this.appendStatementInput("CHILD")
            .setCheck('BLOCK_TOOL_MAIN_XML')
            .setAlign(Blockly.ALIGN_LEFT);
        this.appendDummyInput()
            .setAlign(Blockly.ALIGN_LEFT)
            .appendField("</block>");
        this.setInputsInline(true);
        this.setPreviousStatement(true, 'BLOCK_TOOL_XML_BLOCK');
        this.setNextStatement(true, 'BLOCK_TOOL_XML_BLOCK');
        this.setColour(BLOCK_TOOL_XML_BLOCK_HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    onchange: function () {
        const parent = this.getSurroundParent();
        let check = false;
        if (parent && [
            'block_tool_xml_value',
            'block_tool_xml_statement'
        ].includes(parent.type)) {
            check = true;
        } else {
            check = false;
        }
        if (check) {
            this.setWarningText(null);
        } else {
            this.setWarningText("此块需放到value或statement下面");
        }
    }
};

blocks.block_tool_xml_shadow = {
    init: function () {
        this.appendDummyInput()
            .setAlign(Blockly.ALIGN_LEFT)
            .appendField("<shadow type=\"")
            .appendField(new Blockly.FieldTextInput("math_number"), "TYPE")
            .appendField("\">");
        this.appendStatementInput("CHILD")
            .setCheck('BLOCK_TOOL_MAIN_XML')
            .setAlign(Blockly.ALIGN_LEFT);
        this.appendDummyInput()
            .setAlign(Blockly.ALIGN_LEFT)
            .appendField("</shadow>");
        this.setInputsInline(true);
        this.setPreviousStatement(true, 'BLOCK_TOOL_XML_BLOCK');
        this.setNextStatement(true, 'BLOCK_TOOL_XML_BLOCK');
        this.setColour(BLOCK_TOOL_XML_BLOCK_HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    onchange: function () {
        const parent = this.getSurroundParent();
        let check = false;
        if (parent && [
            'block_tool_xml_value',
            'block_tool_xml_statement'
        ].includes(parent.type)) {
            check = true;
        } else {
            check = false;
        }
        if (check) {
            this.setWarningText(null);
        } else {
            this.setWarningText("此块需放到value或statement下面");
        }
    }
};

blocks.block_tool_xml_field = {
    init: function () {
        this.appendDummyInput()
            .setAlign(Blockly.ALIGN_LEFT)
            .appendField("<field name=\"")
            .appendField(new Blockly.FieldTextInput("NUM"), "NAME")
            .appendField("\">")
            .appendField(new Blockly.FieldTextInput("0"), "VALUE")
            .appendField("</field>");
        this.setInputsInline(true);
        this.setPreviousStatement(true, 'BLOCK_TOOL_MAIN_XML');
        this.setNextStatement(true, 'BLOCK_TOOL_MAIN_XML');
        this.setColour(BLOCK_TOOL_XML_FIELD_HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    onchange: function () {
        const parent = this.getSurroundParent();
        let check = false;
        if (parent && [
            'block_tool_main',
            'block_tool_xml_block',
            'block_tool_xml_shadow'
        ].includes(parent.type)) {
            check = true;
        } else {
            check = false;
        }
        if (check) {
            this.setWarningText(null);
        } else {
            this.setWarningText("此块需放到XML、block或shadow下面");
        }
    }
};

export default blocks;